package cn.getech.data.development.controller;

import cn.getech.data.development.constant.DataDevelopmentBizExceptionEnum;
import cn.getech.data.development.model.dto.RealtimeResourceDto;
import cn.getech.data.development.model.qo.RealtimeResourceSearchQO;
import cn.getech.data.development.model.req.realtimeresource.RealtimeResourceDelReq;
import cn.getech.data.development.model.req.realtimeresource.RealtimeResourceListReq;
import cn.getech.data.development.model.res.realtimeresource.RealtimeResourceListRes;
import cn.getech.data.development.model.vo.RealtimeResourceVO;
import cn.getech.data.development.service.ProcUserService;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.*;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.data.intelligence.common.validator.group.AddGroup;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.utils.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.getech.data.development.entity.RealtimeResourceEntity;
import cn.getech.data.development.service.RealtimeResourceService;
import org.springframework.web.multipart.MultipartFile;
import scala.annotation.meta.param;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 实时资源管理
 *
 * @author qiuxinglei
 * @email qiuxinglei@gmail.com
 * @date 2020-09-02 11:14:22
 */
@Api(value="实时资源管理",tags={"实时资源管理"})
@RestController
@RequestMapping("/development/realtimeresource")
public class RealtimeResourceController {
    @Autowired
    private RealtimeResourceService realtimeResourceService;
    @Autowired
    private ProcUserService procUserService;


    @ApiOperation("所有的资源文件")
    @PostMapping("/listAll")
    //@SysLog(value="所有的资源文件",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public R listAll(@RequestBody RealtimeResourceSearchQO  realtimeResourceSearchQO){
        List<RealtimeResourceDto> lists = realtimeResourceService.listAll(realtimeResourceSearchQO);
        return R.okWithData(PojoUtils.listConvert(RealtimeResourceVO.class,lists));
    }

    @ApiOperation("列表")
    @PostMapping("/list")
    //@SysLog(value="列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public PageRes<PageUtils2<RealtimeResourceListRes>> list(@RequestBody RealtimeResourceListReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils2<RealtimeResourceListRes> page = realtimeResourceService.queryPage(req, ShiroUtils.getUserId());

        return BaseRes.okWithPage(page);
    }

    @ApiOperation("项目内的资源列表")
    @PostMapping("/listByProc")
    //@SysLog(value="项目内的资源列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public PageRes<PageUtils2<RealtimeResourceListRes>> listByProc(@RequestBody RealtimeResourceListReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        PageUtils2<RealtimeResourceListRes> page = realtimeResourceService.queryPageByProc(req, ShiroUtils.getUserId());
        return BaseRes.okWithPage(page);
    }


    @ApiOperation("信息")
    @GetMapping("/info/{resourceId}")
    @SysLog(value="资源文件详情",type= OperationTypeEnum.GETCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public DataRes<RealtimeResourceEntity> info(@PathVariable("resourceId") Long resourceId){
        RealtimeResourceEntity realtimeResource = realtimeResourceService.getById(resourceId);

        return BaseRes.okWithData(realtimeResource);
    }

    @ApiOperation("保存")
    @PostMapping("/save")
    @SysLog(value="保存资源文件",type= OperationTypeEnum.UPLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public BaseRes save(
                        @ApiParam(name = "procId", value = "项目id", required = true)@RequestParam("procId") Long procId,
                        @ApiParam(name = "resourceName", value = "资源名称", required = true)@RequestParam("resourceName") String resourceName,
                        @ApiParam(name = "fileExtension", value = "文件类型", required = true)@RequestParam("fileExtension") String fileExtension,
                        @ApiParam(name = "resorurceDesc", value = "描述", required = true)@RequestParam("resourceDesc") String resourceDesc,
                        @ApiParam(name = "typeResource", value = "资源类型")@RequestParam("typeResource") Integer typeResource,
                        @ApiParam(name = "file", value = "上传资源", required = true)@RequestParam(value = "file") MultipartFile file){
        if(!procUserService.checkViewPermission(procId.intValue())){
            throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
        }
        Long resourceId = realtimeResourceService.save(procId, resourceName, fileExtension, resourceDesc, typeResource, file, ShiroUtils.getUserId());
        return BaseRes.okWithData(resourceId);
    }

    @ApiOperation("删除")
    @PostMapping("/delete")
    @SysLog(value="删除资源文件",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public BaseRes delete(@RequestBody RealtimeResourceDelReq req){
        ValidatorUtils.validateEntity(req, AddGroup.class);
        if(req.getProcId() != null && req.getProcId() != 0){
            if(!procUserService.checkViewPermission(req.getProcId())){
                throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
            }
        }
        realtimeResourceService.deleteResource(req.getResourceId(), ShiroUtils.getUserId());

        return BaseRes.ok();
    }

    @ApiOperation("下载")
    @GetMapping("/download")
    @SysLog(value="下载资源",type= OperationTypeEnum.DOWNLOADCODE, modulesType = OperationModulesEnum.BDPMODELCODE,platformModuleType = ModuleEnum.PROJECT_LIST_MODULE_CODE)
    public void download(@RequestParam Long resourceId,@RequestParam(required = false) Integer procId, HttpServletResponse response) throws Exception {
        if(procId != null && procId != 0){
            if(!procUserService.checkViewPermission(procId)){
                throw new RRException(DataDevelopmentBizExceptionEnum.PROCINFO_PERMISSIOIN.getMessage());
            }
        }
        realtimeResourceService.download(resourceId, response);
    }

}
